﻿RequestsApp.service('repositoryConfigurator', function ($q, $rootScope) {
    this.verifyRepositoryConfiguration = function () {
        var deferred = $q.defer();

        var context = new SP.ClientContext($rootScope.AppWebUrl);
        var appContextSite = new SP.AppContextSite(context, $rootScope.HostWebUrl);
        var web = appContextSite.get_web();
        context.load(web);

        //check list exists
        var lists = web.get_lists();
        context.load(lists);
        context.executeQueryAsync(
            function () {
                var isListAvail = false;
                var listEnumerator = lists.getEnumerator();
                while (listEnumerator.moveNext()) {
                    list = listEnumerator.get_current();
                    if (list.get_title() == $rootScope.RequestsListName) {
                        isListAvail = true;
                    }
                }
                if (isListAvail != true) {
                    //create the list
                    var listCreationInfo = new SP.ListCreationInformation();
                    listCreationInfo.set_title($rootScope.RequestsListName);
                    listCreationInfo.set_description("Requests list description");
                    listCreationInfo.set_templateType(SP.ListTemplateType.genericList);

                    var list = web.get_lists().add(listCreationInfo);

                    context.load(list);
                    //TODO: choice fields must be taxonomy fields
                    var newCols = [
                      '<Field Name="Approvers" DisplayName="Approvers" Type="UserMulti" Required="TRUE" Mult="TRUE" />',
                      '<Field Name="Country" DisplayName="Country" Type="Choice" Required="TRUE"><CHOICES><CHOICE>Russia</CHOICE><CHOICE>Ukraine</CHOICE><CHOICE>Kazakhstan</CHOICE></CHOICES></Field>',
                      '<Field Name="City" DisplayName="City" Type="Text" Required="TRUE" />',
                      '<Field Name="Company" DisplayName="Company" Type="Text" Required="TRUE" />',
                      '<Field Name="Purpose" DisplayName="Purpose" Type="Choice" Required="TRUE"><CHOICES><CHOICE>Training</CHOICE><CHOICE>Purpose 1</CHOICE><CHOICE>Purpose 2</CHOICE></CHOICES></Field>',
                      '<Field Name="FinYear" DisplayName="FinYear" Type="Choice" Required="TRUE"><CHOICES><CHOICE>2010</CHOICE><CHOICE>2011</CHOICE><CHOICE>2012</CHOICE><CHOICE>2013</CHOICE><CHOICE>2014</CHOICE></CHOICES></Field>',
                      '<Field Name="Training" DisplayName="Training" Type="Choice" Required="FALSE"><CHOICES><CHOICE>ACCA</CHOICE><CHOICE>Training 1</CHOICE><CHOICE>Training 2</CHOICE></CHOICES></Field>',
                      '<Field Name="Details" DisplayName="Details" Type="Note" Required="TRUE" />',
                      '<Field Name="Travelers" DisplayName="Travelers" Type="UserMulti" Required="TRUE" Mult="TRUE" />'];

                    var numberCols = newCols.length;
                    for (var i = 0; i < numberCols; i++) {
                        var newColumn = list.get_fields().addFieldAsXml(newCols[i], true, 28);
                        context.load(newColumn);
                    }

                    context.executeQueryAsync(
                        function () {
                            $rootScope.$apply(function () {
                                deferred.resolve();
                            });
                        },
                        function (sender, args) {
                            $rootScope.$apply(function () {
                                deferred.reject('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                            });
                        }
                    );
                }
                else {
                    $rootScope.$apply(function () {
                        deferred.resolve();
                    });
                }
            },
            function (sender, args) {
                $rootScope.$apply(function () {
                    deferred.reject('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                });
            }
        );

        return deferred.promise;
    };
});